package gang.org.springframework.framework.core.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class GangAnnotationsScanner {

    private static final Map<AnnotatedElement, Annotation[]> declaredAnnotationCache = new ConcurrentHashMap<>(256);

    public static boolean hasPlianJavaAnnotationOnly(Class<?> type){
        return (type.getName().startsWith("java.") || type == GangOrdered.class);
    }

    public static <A extends Annotation> A getDeclaredAnnotation(AnnotatedElement source, Class<A> annotationType)
    {
        Annotation[] annotations = getDeclaredAnnotations(source, false);

        for (Annotation annotation : annotations) {

            if (annotations != null && annotationType == annotation.annotationType()) {

                return (A) annotation;
            }

        }

        return null;
    }

    public static Annotation[] getDeclaredAnnotations(AnnotatedElement source, boolean defensive)
    {
        Annotation[] annotations = declaredAnnotationCache.get(source);
        if (false){
            //TODO nothing
        }else {
            annotations = source.getDeclaredAnnotations();
            if (annotations.length != 0) {
                //TODO nothing
            }
        }
        return annotations;
    }
}
